#include "form_legend_dlg.h"
#include "ui_form_legend_dlg.h"

// add necessary includes here
#include "utils/defines/def_business_rules.h"
#include "utils/keyboard/keyboard_mgr.h"

#define DF_LED_TYPE_SIZE     4

/*
 * Function:    FormLegendDialog
 * Description: The default constructor
 * Paramer:     void
 * Return:
 */
FormLegendDialog::FormLegendDialog(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormLegendDialog)
{
    qDebug() << Q_FUNC_INFO;

    ui->setupUi(this);

    // After constructing object, initialize the object
    _initialze();

    // Initialize control's UI
    _initUi();

    // Hide widget firstly
    hide();
}

/*
 * FormLegendDialog:    ~FormLegendDialog
 * Description: The default destructor
 * Paramer:     void
 * Return:
 */
FormLegendDialog::~FormLegendDialog()
{
    qDebug() << Q_FUNC_INFO;

    delete ui;
}

/*
 * Function:    _Initialze
 * Description: After constructing object, initialize the object
 * Paramer:     void
 * Return:      void
 */
void FormLegendDialog::_initialze()
{
    qDebug() << Q_FUNC_INFO;

    // Signal-slot initialization
    std::vector<bool> vecConnect;
    vecConnect.push_back(QObject::connect(this, SIGNAL(signalRefreshUI(int, int, QStringList)),
                                          this, SLOT(slotRefreshUI(int, int, QStringList)),
                                          static_cast<Qt::ConnectionType>(Qt::AutoConnection | Qt::UniqueConnection)));
    for(int i=0; i<(int)vecConnect.size(); i++) {
        Q_ASSERT(vecConnect.at(i));
    }
}

/*
* Function:		_InitUi
* Description:	Init the window style
* Paramer:		void
* Return:		void
*/
void FormLegendDialog::_initUi()
{
    qDebug() << Q_FUNC_INFO;

    // Displays the background image of the window
    QWidget::setAttribute(Qt::WA_StyledBackground);

    SET_STYLE_SHEET(QString(":/%1/share/%2.qss").arg("skin_1").arg(this->objectName()));

    QValidator *vldDigit = new QRegExpValidator(QRegExp("^-?[0-9]{7}$"));
    ui->lineEdit_MinValueY->setValidator(vldDigit);
    ui->lineEdit_MaxValueY->setValidator(vldDigit);
    ui->lineEdit_MinValueY->setInputMethodHints(Qt::ImhDigitsOnly);
    ui->lineEdit_MaxValueY->setInputMethodHints(Qt::ImhDigitsOnly);
}

/*
* Function:		changeEvent
* Description:	Triggers an event before it is switch language
* Paramer:		QShowEvent *event - the pointer of "QEvent"
* Return:		void
*/
void FormLegendDialog::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);

    switch (e->type())
    {
    case QEvent::LanguageChange:
        qDebug() << Q_FUNC_INFO;

        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

/*
* Function:		on_pushButton_Confirm_clicked
* Description:	Save data after click the button
* Paramer:		void
* Return:		void
*/
void FormLegendDialog::on_pushButton_Confirm_clicked()
{
    qDebug() << Q_FUNC_INFO;

    QStringList lstAxisRange =  QStringList() << ui->lineEdit_MinValueY->text().trimmed() << ui->lineEdit_MaxValueY->text().trimmed();

    emit signalReplayInfo(m_nChartType, m_nChartAttr, 0, lstAxisRange);

    QTimer::singleShot(200, this, [&]() {
        this->hide();
    });
}

/*
* Function:		on_pushButton_Cancel_clicked
* Description:	Close the window after click the button
* Paramer:		void
* Return:		void
*/
void FormLegendDialog::on_pushButton_Cancel_clicked()
{
    qDebug() << Q_FUNC_INFO;

    ui->lineEdit_MinValueY->setText("");
    ui->lineEdit_MaxValueY->setText("");

    this->hide();
}

/*
 * Function:    slotRefreshUI
 * Description: Display window
 * Paramer:     QStringList lstStatusFlags - line status falsgs
 *              QStringList lstAxisRange - Y axis range
 * Return:      void
 */
void FormLegendDialog::slotRefreshUI(int nChartType, int nChartAttribute, QStringList lstAxisRange)
{
    qDebug() << Q_FUNC_INFO << nChartType << nChartAttribute << lstAxisRange;
    m_nChartType = nChartType;
    m_nChartAttr = nChartAttribute;

    if(E_CA_AXIS_RANGE == nChartAttribute) {
        if(2 == lstAxisRange.size()) {
            ui->lineEdit_MinValueY->setText(lstAxisRange.at(0));
            ui->lineEdit_MaxValueY->setText(lstAxisRange.at(1));
        }
    }

    KeyboardMgr::getInstance(this)->setParent(this);
    this->setFocus();
    this->show();
}
